package com.xxtx.trade.common.util;

import com.xxtx.trade.web.SslUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class AcceptURLUtils {

    public static String getJSON2Content(String strURL,String acceptType) throws Exception {
        URL url = new URL(strURL);
        if("https".equalsIgnoreCase(url.getProtocol())){
            SslUtils.ignoreSsl();
        }
        HttpURLConnection httpConn = (HttpURLConnection)url.openConnection();
        httpConn.setRequestMethod(acceptType);
        httpConn.setRequestProperty("Content-type","application/x-www-form-urlencoded");
        httpConn.connect();
        BufferedReader reader = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
        String line;
        StringBuffer buffer = new StringBuffer();
        while ((line = reader.readLine()) != null) {
            buffer.append(line);
        }
        reader.close();
        httpConn.disconnect();
        return buffer.toString();
    }



    public static String getToken(String strURL,String AccessKey,String Nonce,String Timestamp,String Sign) throws Exception {
        URL url = new URL(strURL);
        if("https".equalsIgnoreCase(url.getProtocol())){
            SslUtils.ignoreSsl();
        }
        HttpURLConnection httpConn = (HttpURLConnection)url.openConnection();
        httpConn.setRequestMethod("GET");
        httpConn.setRequestProperty("Content-type","application/x-www-form-urlencoded");
        httpConn.setRequestProperty("AccessKey",AccessKey);
        httpConn.setRequestProperty("Nonce",Nonce);
        httpConn.setRequestProperty("Timestamp",Timestamp);
        httpConn.setRequestProperty("Sign",Sign);
        httpConn.connect();
        BufferedReader reader = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
        String line;
        StringBuffer buffer = new StringBuffer();
        while ((line = reader.readLine()) != null) {
            buffer.append(line);
        }
        reader.close();
        httpConn.disconnect();
        return buffer.toString();
    }
}
